package webreg.model.swing;

import java.util.Iterator;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import org.apache.log4j.Logger;

import webreg.model.AgeTable;
import webreg.model.container.AgeTableContainer;
import webreg.model.exceptions.EmptyDescriptionException;

public class AgeTableContainerComboBoxModel extends AgeTableContainer implements ComboBoxModel, ListDataListener {
	private int mSelected;
	private static Logger logger = Logger.getLogger( AgeTableContainerComboBoxModel.class );
	private Vector<ListDataListener> mListeners;
	
	public AgeTableContainerComboBoxModel() {
		super();
		mSelected = 0;
		mListeners = new Vector<ListDataListener>();
	}
	
	public AgeTableContainerComboBoxModel(Vector<AgeTable> ageTables) {
		super(ageTables);
		mSelected = 0;
		mListeners = new Vector<ListDataListener>();
	}
		
	public AgeTableContainerComboBoxModel(AgeTableContainerComboBoxModel toCopy) {
		super(toCopy);
		mSelected = 0;
		mListeners = new Vector<ListDataListener>();		
	}
	
	public void addElementAt(int arg0, AgeTable inTable) {
		super.getAgeTables().add(arg0, inTable);
		intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, arg0, arg0));
	}		
	
	public void editElementAt(int arg0, String inTitle) throws EmptyDescriptionException {
		super.getAgeTables().elementAt(arg0).setName(inTitle);
		contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, arg0, arg0));
	}	
	
	public void removeElementAt(int arg0) {
		super.getAgeTables().remove(arg0);
		if (arg0 > 0) {
			mSelected = mSelected - 1;
		}
		intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, arg0, arg0));
	}			
	
	
	@Override
	public boolean equals(AgeTableContainer toCompare) {
		return super.equals(toCompare);
	}

	@Override
	public Object getSelectedItem() {
		return super.getAgeTables().elementAt(mSelected);
	}

	@Override
	public void setSelectedItem(Object arg0) {
		if (! (arg0 instanceof AgeTable)) {
			logger.warn("No agetable object given!");
		}
		AgeTable toSearch = (AgeTable)arg0;
		int i = 0;
		mSelected = i;
		for(Iterator<AgeTable> iter = super.getAgeTables().iterator(); iter.hasNext(); ) {
			AgeTable inList = (AgeTable) iter.next();		
			if ( toSearch.equals(inList) ) {
				mSelected = i;
				return;
			}
			i++;
		}
	}

	@Override
	public void addListDataListener(ListDataListener arg0) {
		mListeners.add(arg0);
	}

	@Override
	public Object getElementAt(int arg0) {
		return super.getAgeTables().elementAt(arg0);
	}

	@Override
	public int getSize() {
		return super.getAgeTables().size();
	}

	@Override
	public void removeListDataListener(ListDataListener arg0) {
		mListeners.remove(arg0);
	}

	@Override
	public void contentsChanged(ListDataEvent e) {
		for(Iterator<ListDataListener> iter = mListeners.iterator(); iter.hasNext(); ) {
			ListDataListener inList = (ListDataListener) iter.next();
			inList.contentsChanged(e);
		}		
	}

	@Override
	public void intervalAdded(ListDataEvent e) {
		for(Iterator<ListDataListener> iter = mListeners.iterator(); iter.hasNext(); ) {
			ListDataListener inList = (ListDataListener) iter.next();
			inList.intervalAdded(e);
		}		
	}

	@Override
	public void intervalRemoved(ListDataEvent e) {
		for(Iterator<ListDataListener> iter = mListeners.iterator(); iter.hasNext(); ) {
			ListDataListener inList = (ListDataListener) iter.next();
			inList.intervalRemoved(e);
		}		
	}

}
